//
//  NumberAndColors.swift
//  MakeMoney
//
//  Created by Fan ZhiQiang on 2024/12/9.
//

import SwiftUI

struct NumberAndColors: ViewModifier {
    let color: Color
    func body(content: Content) -> some View {
        content
            .font(.title2)
            .foregroundStyle(.white)
            .frame(width: 40, height: 40)
            .background(color)
            .clipShape(.circle)
            .shadow(radius: 2 , x: 2, y: 3)
            
    }
}
//ViewModifier协议
struct NumberAndStatistics: ViewModifier {
    let number: Int
    let count: Int
    func body(content: Content) -> some View {
        
    }
}
//AlignmentID协议
struct MyAlignment: AlignmentID {
    static func defaultValue(in context: ViewDimensions) -> CGFloat {
        context.height / 3.0
    }
}
extension VerticalAlignment {
    static let firstThird = VerticalAlignment(MyAlignment.self)
}
//写法二
extension VerticalAlignment {
    enum BusAlignment: AlignmentID {
        static func defaultValue(in context: ViewDimensions) -> CGFloat {
            context[VerticalAlignment.center]
        }
    }
    static let busAlignment = VerticalAlignment(BusAlignment.self)
}
//自定义布局: 配合AnyLaout布局切换视图，实现自定义动画
struct MyLayout: Layout {
    func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
        var totalWidth: CGFloat = 0
        var totalHeight: CGFloat = 0
        for(index, view) in subviews.enumerated() {
            if index > 0 {
                totalHeight += 10
            }
            let viewSize = view.sizeThatFits(.unspecified)
            totalWidth += viewSize.width + CGFloat(30 * index)
            totalHeight += viewSize.height
        }
        return CGSize(width: totalWidth, height: totalHeight)
    }
    
    func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
        var poX: CGFloat = bounds.origin.x
        var poY: CGFloat = bounds.origin.y
        for (index, view) in subviews.enumerated() {
            if index > 0 {
                poY += 10
                poX += 30
            }
            view.place(at: CGPoint(x: poX, y: poY), proposal: .unspecified)
            poY += view.sizeThatFits(.unspecified).height
        }
    }
}
//按钮样式协议
struct MyButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        let pressed = configuration.isPressed
        return configuration.label
            .padding()
            .border(Color.green, width: 5)
            .scaleEffect(pressed ? 1.2 : 1.0)
    }
}
//ToggleStyle
struct MyToggleStyle: ToggleStyle {
    func makeBody(configuration: MyToggleStyle.Configuration) -> some View {
        HStack(alignment: .center) {
            configuration.label
            Spacer()
            Image(systemName: "chenkmark")
                .font(.largeTitle)
                .foregroundStyle(configuration.isOn ? Color.green : Color.gray)
                .onTapGesture {
                    configuration.$isOn.wrappedValue.toggle()
                }
        }
    }
}
//TextSelection
struct TextSelectionView: View {
    @State private var text = ""
    @State private var selection: TextSelection? = nil
    var body: some View {
        VStack {
            TextField("Inter text", text: $text, selection: $selection, axis: .vertical)
                .textFieldStyle(.roundedBorder)
                .padding(20)
                .lineLimit(5)
            Text(getSelection() ?? "")
        }
    }
    func getSelection() -> String? {
        if let indices = selection?.indices {
            if case .selection(let range) = indices {
                let subString = text[range]
                return String(subString)
            }
        }
        return nil
    }
}
